package com.wencai.test;

import com.wencai.bean.ConvertedStock;
import com.wencai.bean.DateIncrement;
import com.wencai.bean.NativeStock;
import com.wencai.bean.WriteOutStock;
import com.wencai.util.ExportUtils;
import com.wencai.util.Import;

import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author wangpan
 * @date 2023/4/17
 */
public class IndexIncrement {
    private static String branchFilePath = "C:\\Users\\Administrator\\Downloads\\行业板块\\";

    private static String branchOutPath = "C:\\Users\\Administrator\\Downloads\\轮动表\\行业轮动.xlsx";

    private static String conceptFilePath = "C:\\Users\\Administrator\\Downloads\\概念板块\\";

    private static String conceptOutPath = "C:\\Users\\Administrator\\Downloads\\轮动表\\概念轮动.xlsx";


    public static void main(String[] args) throws IOException {
        export(branchFilePath, branchOutPath);

        export(conceptFilePath, conceptOutPath);
    }


    private static void export(String dir, String outPath) throws IOException {
        File f = new File(dir);
        String[] list = f.list();
        Map<String, ConvertedStock> stockMap = new HashMap<>();
        for (String child : list) {
            if (child.endsWith("xlsx")) {
                Map<String, List<NativeStock>> data = Import.getData(dir + child);
                for (Map.Entry<String, List<NativeStock>> entry : data.entrySet()) {
                    String date = entry.getKey();
                    for (NativeStock nativeStock : entry.getValue()) {
                        String name = nativeStock.getName();
                        Double increment = nativeStock.getIncrement();
                        ConvertedStock convertedStock = stockMap.get(name);
                        if (convertedStock == null) {
                            convertedStock = new ConvertedStock(name, 1);
                        } else {
                            convertedStock.setTimes(convertedStock.getTimes() + 1);
                        }
                        convertedStock.add(new DateIncrement(date, increment));
                        convertedStock.sort();
                        convertedStock.setMaxIncrement();
                        stockMap.put(name, convertedStock);
                    }
                }
            }
        }
        List<ConvertedStock> values = new ArrayList<>(stockMap.values());
        List<WriteOutStock> collect = values.stream().map(item -> {
            String secDate = null;
            Double secIncrement = null;
            String lastDate = null;
            Double lastIncrement = null;
            if (item.getList().size() > 1) {
                secDate = item.getList().get(1).getDate();
                secIncrement = item.getList().get(1).getIncrement();
                DateIncrement lastDateIncrement = item.getList().get(item.getList().size() - 1);
                lastDate = lastDateIncrement.getDate();
                lastIncrement = lastDateIncrement.getIncrement();
            }
            return new WriteOutStock(item.getName(), item.getTimes(), item.getMaxIncrement(), item.getList().get(0).getDate(), item.getList().get(0).getIncrement(), secDate, secIncrement, lastDate, lastIncrement);
        }).collect(Collectors.toList());
        Collections.sort(collect);
        ExportUtils.exportIncrement(collect, WriteOutStock.class, outPath);
    }
}
